﻿using BCCommon;
using BCCommon.TMS.Order;
using BCData.TMS.Order.OrderExtention;
using BCEntity.Common.EntityNameConst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.Order.OrderExtention
{
    public class OrderExtentionService : IOrderExtentionService
    {
        private readonly IOrderExtentionData orderExtentionData;
        private readonly IRedisService redisService;

        public OrderExtentionService(IOrderExtentionData orderExtentionData, IRedisService redisService)
        {
            this.orderExtentionData = orderExtentionData;
            this.redisService = redisService;
        }

        public bool UpdateOrderStatusAcceptance(string orderNo, long orderExtentionId)
        {
            var result = this.orderExtentionData.UpdateOrderStatusAcceptance(orderNo, orderExtentionId).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusArrived(string orderNo, long orderExtentionId)
        {
            var result = this.orderExtentionData.UpdateOrderStatusArrived(orderNo, orderExtentionId).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusCancel(string orderNo)
        {
            var result = this.orderExtentionData.UpdateOrderStatusCancel(orderNo).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusComplete(string orderNo, OrderPayer payer)
        {
            return false;
            //var result = this.orderExtentionData.UpdateOrderStatusComplete(orderNo, payer).GetAwaiter().GetResult() > 0;
            //if (result)
            //{
            //    RemoveOrderExtentionCatch(orderNo);
            //}
            //return result;
        }

        public bool UpdateOrderStatusDispatching(string orderNo)
        {
            var result = this.orderExtentionData.UpdateOrderStatusDispatching(orderNo).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusPickup(string orderNo)
        {
            var result = this.orderExtentionData.UpdateOrderStatusPickup(orderNo).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusPlaceOrder(string orderNo)
        {
            var result = this.orderExtentionData.UpdateOrderStatusPlaceOrder(orderNo).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusTransporting(string orderNo, long orderExtentionId)
        {
            var result = this.orderExtentionData.UpdateOrderStatusTransporting(orderNo, orderExtentionId).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        public bool UpdateOrderStatusTransporting(string orderNo)
        {
            var result = this.orderExtentionData.UpdateOrderStatusTransporting(orderNo).GetAwaiter().GetResult();
            if (result)
            {
                RemoveOrderExtentionCatch(orderNo);
            }
            return result;
        }

        #region 移除订单缓存
        private void RemoveOrderExtentionCatch(string orderNo)
        {

            string key = string.Format("{0}:OrderNo={1}", EntityNameConst.OrderExtentionEntities, orderNo);
            this.redisService.DeleteAsync(key);
        }
        #endregion
    }
    }
